package com.modifiertech.lineage.network;

import com.modifiertech.lineage.BaseConst;
import com.modifiertech.lineage.LineageRegistries;
import com.modifiertech.lineage.api.PlayerData;
import com.modifiertech.lineage.event.PlayerDataHandler;
import com.modifiertech.lineage.event.SkillShortCutBindEvent;
import com.modifiertech.lineage.handler.SkillLevelUpHandler;
import com.modifiertech.lineage.handler.SkillShortCutBindHandler;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

/**
 * ClassName: MessageSkillModify <br/>
 * Description: <br/>
 * date: 2020/8/28 21:43<br/>
 *
 * @author Administrator<br />
 * @since JDK 1.8
 */
public class MessageSkillModify implements IMessage, IMessageHandler<MessageSkillModify,IMessage> {
    private NBTTagCompound nbtTagCompound=new NBTTagCompound();

    @Override
    public void fromBytes(ByteBuf byteBuf) {
        nbtTagCompound= ByteBufUtils.readTag(byteBuf);
    }

    @Override
    public void toBytes(ByteBuf byteBuf) {
        ByteBufUtils.writeTag(byteBuf,nbtTagCompound);
    }

    @Override
    public IMessage onMessage(MessageSkillModify messageSkillModify, MessageContext messageContext) {
        FMLCommonHandler.instance().getMinecraftServerInstance().addScheduledTask(()->{
            handleMessage(messageSkillModify,messageContext);
        });
        return null;
    }
    public void handleMessage(MessageSkillModify messageSkillModify, MessageContext messageContext){
        PlayerData playerData= PlayerDataHandler.get(messageContext.getServerHandler().player);
        messageSkillModify.getNbtTagCompound().getKeySet().forEach(
                skillName->{
                    messageSkillModify.getNbtTagCompound().getCompoundTag(skillName).getKeySet().forEach(operate->{
                        //处理升级事件
                        //技能名/升级/升级等级
                        if(BaseConst.SkillOperate.up.name().equals(operate)){
                            SkillLevelUpHandler.INSTANCE
                                    .skillLevelUp(messageContext.getServerHandler().player,skillName
                                    ,messageSkillModify.getNbtTagCompound().getCompoundTag(skillName).getInteger(operate),
                                            playerData);
                        //处理技能绑定事件
                        //技能名/绑定/绑定快捷键index
                        }else if (BaseConst.SkillOperate.bind_shortCut.name().equals(operate)){
                            SkillShortCutBindHandler.INSTANCE
                                    .skillShortCutBind(messageContext.getServerHandler().player,skillName
                                            ,messageSkillModify.getNbtTagCompound().getCompoundTag(skillName).getInteger(operate),
                                            playerData);
                        }
                    });
        });





    }

    public NBTTagCompound getNbtTagCompound() {
        return nbtTagCompound;
    }

    public void setNbtTagCompound(NBTTagCompound nbtTagCompound) {
        this.nbtTagCompound = nbtTagCompound;
    }
    //构造技能升级NBT
    public synchronized MessageSkillModify  write(String skillName, BaseConst.SkillOperate operate, int num){
        ResourceLocation skillResource=new ResourceLocation(BaseConst.MODID,skillName);
        if(!LineageRegistries.SKILLS.containsKey(skillResource))return this;
        //INIT
        if(!this.getNbtTagCompound().hasKey(skillName)){
            this.getNbtTagCompound().setTag(skillName,new NBTTagCompound());
        }
        //INIT
        if(!this.getNbtTagCompound().getCompoundTag(skillName).hasKey(operate.name())){
            this.getNbtTagCompound().getCompoundTag(skillName).setInteger(operate.name(),num);
        }else{
            this.getNbtTagCompound().getCompoundTag(skillName).setInteger(operate.name(),
                    this.getNbtTagCompound().getCompoundTag(skillName).getInteger(operate.name())+num);
        }
        return this;
    }
    public synchronized MessageSkillModify  writeClearTag(int num){
        //INIT
        if(!this.getNbtTagCompound().hasKey("-")){
            this.getNbtTagCompound().setTag("-",new NBTTagCompound());
        }
        //INIT
        if(!this.getNbtTagCompound().getCompoundTag("-").hasKey(BaseConst.SkillOperate.bind_shortCut.name())){
            this.getNbtTagCompound().getCompoundTag("-").setInteger(BaseConst.SkillOperate.bind_shortCut.name(),num);
        }else{
            this.getNbtTagCompound().getCompoundTag("-").setInteger(BaseConst.SkillOperate.bind_shortCut.name(),
                    this.getNbtTagCompound().getCompoundTag("-").getInteger(BaseConst.SkillOperate.bind_shortCut.name())+num);
        }
        return this;
    }

}
